import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class Recorder {
	String tag = null;

	File Penalty_file = null;
	BufferedWriter Penalty_file_bw = null;

	File D_file = null;
	BufferedWriter D_file_bw = null;

	File log_file = null;
	BufferedWriter log_file_bw = null;

	File queue_length_file = null;
	BufferedWriter queue_length_file_bw = null;

	File energy_file = null;
	BufferedWriter energy_file_bw = null;

	File delay_file = null;
	BufferedWriter delay_file_bw = null;

	File metric_file = null;
	BufferedWriter metric_file_bw = null;

	File delay_per_byte_file = null;
	BufferedWriter delay_per_byte_file_bw = null;

	File send_data_file = null;
	BufferedWriter send_data_file_bw = null;

	File v_file = null;
	BufferedWriter v_file_bw = null;

	File overdue_file = null;
	BufferedWriter overdue_file_bw = null;

	static File v_gp_bp_file = null;
	static BufferedWriter v_gp_bp_file_bw = null;
	// 记录v_gp_bp_file三元组的数据单元个数的相关变量
	static {
		v_gp_bp_file = new File("C:\\Users\\LZQ\\Desktop\\Lypunov_simulation_data\\tail\\v_p_file-w-1-1-1" + ".csv");
		try {
			if (!v_gp_bp_file.exists())
				v_gp_bp_file.createNewFile();
			v_gp_bp_file_bw = new BufferedWriter(new FileWriter(v_gp_bp_file, true));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public Recorder(String t) {
		tag = t;
		// 记录Penalty的相关变量
//		Penalty_file = new File("C:\\Users\\LZQ\\Desktop\\Lypunov_simulation_data\\penalty" + tag + ".csv");
//		if (Penalty_file.exists())
//			Penalty_file.delete();
//		try {
//			Penalty_file.createNewFile();
//			Penalty_file_bw = new BufferedWriter(new FileWriter(Penalty_file));
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//
//		// 记录决策变量D的相关变量
//		D_file = new File("C:\\Users\\LZQ\\Desktop\\Lypunov_simulation_data\\D" + tag + ".csv");
//		if (D_file.exists())
//			D_file.delete();
//		try {
//			D_file.createNewFile();
//			D_file_bw = new BufferedWriter(new FileWriter(D_file));
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//
		// 记录文件的相关变量
		log_file = new File("C:\\Users\\LZQ\\Desktop\\Lypunov_simulation_data\\log" + tag + ".txt");
		if (log_file.exists())
			log_file.delete();
		try {
			log_file.createNewFile();
			log_file_bw = new BufferedWriter(new FileWriter(log_file, true));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
//
//		// 记录总队长的相关变量
//		queue_length_file = new File("C:\\Users\\LZQ\\Desktop\\Lypunov_simulation_data\\queue_length" + tag + ".csv");
//		if (queue_length_file.exists())
//			queue_length_file.delete();
//		try {
//			queue_length_file.createNewFile();
//			queue_length_file_bw = new BufferedWriter(new FileWriter(queue_length_file));
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//
		// 记录总能耗的相关变量
		energy_file = new File("C:\\Users\\LZQ\\Desktop\\Lypunov_simulation_data\\energy" + tag + ".csv");
		if (energy_file.exists())
			energy_file.delete();
		try {
			energy_file.createNewFile();
			energy_file_bw = new BufferedWriter(new FileWriter(energy_file));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
//
//		// 记录时延的相关变量
//		delay_file = new File("C:\\Users\\LZQ\\Desktop\\Lypunov_simulation_data\\delay_file" + tag + ".csv");
//		if (delay_file.exists())
//			delay_file.delete();
//		try {
//			delay_file.createNewFile();
//			delay_file_bw = new BufferedWriter(new FileWriter(delay_file));
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//
//		// 记录metric的相关变量
//		metric_file = new File("C:\\Users\\LZQ\\Desktop\\Lypunov_simulation_data\\metric_file" + tag + ".csv");
//		if (metric_file.exists())
//			metric_file.delete();
//		try {
//			metric_file.createNewFile();
//			metric_file_bw = new BufferedWriter(new FileWriter(metric_file));
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//
//		// 记录平均每字节时延的相关变量
//		delay_per_byte_file = new File("C:\\Users\\LZQ\\Desktop\\Lypunov_simulation_data\\delay_per_byte_file" + tag + ".csv");
//		if (delay_per_byte_file.exists())
//			delay_file.delete();
//		try {
//			delay_per_byte_file.createNewFile();
//			delay_per_byte_file_bw = new BufferedWriter(new FileWriter(delay_per_byte_file));
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//
//		// 记录发送的数据总数量的相关变量
//		send_data_file = new File("C:\\Users\\LZQ\\Desktop\\Lypunov_simulation_data\\send_data_file" + tag + ".csv");
//		if (send_data_file.exists())
//			send_data_file.delete();
//		try {
//			send_data_file.createNewFile();
//			send_data_file_bw = new BufferedWriter(new FileWriter(send_data_file));
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//
//		// 记录V的相关变量
//		v_file = new File("C:\\Users\\LZQ\\Desktop\\Lypunov_simulation_data\\v_file" + tag + ".csv");
//		if (v_file.exists())
//			v_file.delete();
//		try {
//			v_file.createNewFile();
//			v_file_bw = new BufferedWriter(new FileWriter(v_file));
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//
//		// 记录超出deadline的数据单元个数的相关变量
//		overdue_file = new File("C:\\Users\\LZQ\\Desktop\\Lypunov_simulation_data\\overdue_file" + tag + ".csv");
//		if (overdue_file.exists())
//			overdue_file.delete();
//		try {
//			overdue_file.createNewFile();
//			overdue_file_bw = new BufferedWriter(new FileWriter(overdue_file));
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}

	}

	public void recordPenalty(double d) {
		try {
			Penalty_file_bw.write("" + d + "\n");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void recordD(double d) {
		try {
			D_file_bw.write("" + d + "\n");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void recordlog(StringBuffer sb) {
		try {
			log_file_bw.write(sb.toString());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void recordQueueLength(double d) {
		try {
			queue_length_file_bw.write("" + d + "\n");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void recordEnergy(double d) {
		try {
			energy_file_bw.write("" + d + "\n");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void recordDelay(double d) {
		try {
			delay_file_bw.write("" + d + "\n");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void recordMetric(double d) {
		try {
			metric_file_bw.write("" + d + "\n");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void recordDelayPerByte(double d) {
		try {
			delay_per_byte_file_bw.write("" + d + "\n");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void recordSendData(double d) {
		try {
			send_data_file_bw.write("" + d + "\n");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void recordV(double d) {
		try {
			v_file_bw.write("" + d + "\n");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void recordOverdue(double d) {
		try {
			overdue_file_bw.write("" + d + "\n");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	static public void recordV_GP_BP(String d) {
		try {
			v_gp_bp_file_bw.write("" + d + "\n");
			v_gp_bp_file_bw.flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void flush() {
		try {
//			Penalty_file_bw.flush();
//			Penalty_file_bw.close();
//
//			D_file_bw.flush();
//			D_file_bw.close();
//
//			queue_length_file_bw.flush();
//			queue_length_file_bw.close();
//
			log_file_bw.flush();
			log_file_bw.close();
//
			energy_file_bw.flush();
			energy_file_bw.close();
//
//			delay_file_bw.flush();
//			delay_file_bw.close();
//
//			metric_file_bw.flush();
//			metric_file_bw.close();
//
//			delay_per_byte_file_bw.flush();
//			delay_per_byte_file_bw.close();
//
//			send_data_file_bw.flush();
//			send_data_file_bw.close();
//
//			v_file_bw.flush();
//			v_file_bw.close();
//
//			overdue_file_bw.flush();
//			overdue_file_bw.close();
//
			v_gp_bp_file_bw.flush();
			v_gp_bp_file_bw.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
